/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.application.action.base;

import cn.hallele.infrastructure.metadata.exception.children.ActionException;
import cn.hallele.infrastructure.metadata.io.User;
import cn.hallele.infrastructure.util.NetUtil;
import cn.hallele.infrastructure.util.bean.LoginUser;
import cn.hallele.ms.portalcenter.integration.common.constants.POCMsgCodeConst;
import cn.hallele.ms.starter.web.framework.service.CustomerActionInitializeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/12/31 21:16
 */
@Slf4j
@Service
public class PortalActionInitializeServiceImpl implements CustomerActionInitializeService {
    /**
     * 初始化上下文时获取用户信息的
     *
     * @return UserInfo
     */
    @Override
    public User getCustomerUserInfo() {
        LoginUser loginUser = NetUtil.getLoginUser();
        if (loginUser == null) {
            log.error("用户未登录");
            throw new ActionException(POCMsgCodeConst.TOKEN_ERROR);
        }
        return User.builder()
                .tenantId(loginUser.getTenantId())
                .id(loginUser.getId())
                .username(loginUser.getUsername())
                .email(loginUser.getEmail())
                .phoneNumber(loginUser.getPhoneNumber())
                .build();
    }
}
